#include "city.h"
#include<QDebug>
city::city():name(""){}
Tinfo::Tinfo(){e=nullptr;}
Tinfo::Tinfo(int newc,city * p,city * q , date newb,date newl)
{
    cost = newc;
    b = newb;
    l = newl;
    if(p!=nullptr)f = p;
    if(q!=nullptr)e = q;
}
Tinfo::Tinfo(const Tinfo & y)
{
    b = y.b;
    l = y.l;
    f = y.f;
    e = y.e;
    cost = y.cost;
}
city::city(const city &c){
   name=c.name;
   for(int i=0;i<2;i++){
       for(int j=0;j<c.paths[i].size();j++){
           paths[i].push_back(c.paths[i][j]);
       }
   }
}
city::city(QString n)
{
    name=n;
}
void city::setname(QString t)
{
    name = t;
}
void city::deletetp(Tinfo * tp)
{
    if(tp == nullptr)return;
    for(int type = 0; type <= 1;type++)
    {
        for(int i = 0 ; i< paths[type].size();i++)
        {
            if(&paths[type][i] == tp)paths[type].erase(paths[type].begin()+i);
        }
    }
}
QString city::getname(){
    return name;
}
QVector<Tinfo *> city::findinfo(QString t,bool type)
{
    QVector<Tinfo *>res;
    qDebug()<<paths[type].size();
    qDebug()<<paths[1-type].size();
    for(int i = 0 ;i < paths[type].size();i++)
    {
        Tinfo tmp = paths[type][i];
        city * tmp2 = tmp.e;
        if(tmp2 == nullptr)qDebug()<<"error";
        if(tmp2 ->name =="")qDebug()<<"error2";
        qDebug()<<tmp2 ->name;
        if(tmp.e->name == t)
        {
            res.push_back(&paths[type][i]);
        }
    }
    qDebug()<<"info size = "<<res.size();
    return res;
}//找出终点城市为t的所有的信息
void city::addinfo(city * e, date bdate,date ldate,int money,bool type)
{
    Tinfo newinfo = Tinfo(money,this,e,bdate,ldate);
    paths[type].push_back(newinfo);
}//ok
void city::set(int index,int newc,city * k,date newb,date newl,bool type)
{
    paths[type][index].cost = newc;
    paths[type][index].b = newb;
    paths[type][index].l = newl;
    paths[type][index].e = k;
}
void city::deleteTinfo(QString t,bool type)
{
    for(int tp = 0;tp <=1; tp++)
    {
        for(int i = 0;i<paths[tp].size();i++)
        {
            if(paths[tp][i].e->name == t)
                paths[tp].erase(paths[tp].begin()+i);
        }
    }
}
QVector<Tinfo *> city::timeinfo(date t,bool type)
{
    QVector<Tinfo *>res;
    for(int i = 0 ;i<paths[type].size();i++)
    {
        if(t<paths[type][i].b)
        {
            res.push_back(&paths[type][i]);
        }
    }
    return res;
}
date city::dfs(date now,city  *p,city * e,bool type,QMap<QString,Tinfo *> & aft,QMap<QString,date> & tmp_cost, date d)//aft记录后驱,e为终点，now为到达p城的时间，ans2为费用答案
{
    //边界条件
    date res(INF,INF);int j = 0;
    if(p->name == e->name)return date(0,0);//到达终点，不需要花时间
    //走不到终点并且没路,会返回INF
    for(int i = 0 ;i<p->paths[type].size();i++)
    {
        if(date(now.hour+(int)type+1,now.minute)<p->paths[type][i].b||now == date(0,0))//p为起点肯定能赶上，若不为起点判断等待后能否赶上
        {
            date check = (now==date(0,0)?p->paths[type][i].b:now);
            date tmp = dfs(p->paths[type][i].l,p->paths[type][i].e,e,type,aft,tmp_cost,d + p->paths[type][i].l - now);//tmp为这条路线的终点到最终点所用的时间
            if(tmp == date(INF,INF))
            {
                if(i==p->paths[type].size()-1&&res == date (INF,INF))/*如果前面都没有能到达终点的路，返回INF*/return date(INF,INF);
                else continue;
            }
            date t =tmp + p->paths[type][i].l-check;//t为走这条路从p城出发所花的时间
            if(t < res && (tmp_cost[p->name] == date(0,0)|| d + t < tmp_cost[p->name] )/*先前不存在路径或者存在花费时间更小的路径*/)
            {
                //记录后驱
                tmp_cost[p->name] = d + t;//式子为走这条路径花费的总共时间
                aft[p->name] =& p->paths[type][i];
                res = t ;
            }
        }
    }
    return res;
}
